package com.ruoyi.bole.integral.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.anno.UserCheck;
import com.ruoyi.bole.integral.domain.BoleIntegralApplication;
import com.ruoyi.bole.integral.service.IBoleIntegralApplicationService;
import com.ruoyi.bole.integral.service.IBoleIntegralSeasonService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分申请Controller
 * 
 * @author ruoyi
 * @date 2025-03-23
 */
@RestController
@RequestMapping("/bole/application")
public class BoleIntegralApplicationController extends BaseController
{
    @Autowired
    private IBoleIntegralApplicationService boleIntegralApplicationService;

    @Autowired
    private ISysUserService iSysUserService;

    @GetMapping(value = {"/batch", "/batch/{ids}"})
    public AjaxResult selectBoleIntegralApplicationByIds(@PathVariable(required = false) List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("请选择要查询的积分申请");
        }
        return AjaxResult.success(boleIntegralApplicationService.selectBoleIntegralApplicationByIds(ids));
    }

//    查询积分排行榜
// BoleIntegralController.java
//@PreAuthorize("@ss.hasPermi('bole:integral:rank')")
@GetMapping("/rank")
public AjaxResult getRank(int offset,int pageSize,Integer seasonId) {
    return AjaxResult.success(boleIntegralApplicationService.getIntegrationRank(offset,pageSize,seasonId));
}
    /**
     * 查询积分申请列表  查询某个用户拥有的积分
     */
//    @PreAuthorize("@ss.hasPermi('bole:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoleIntegralApplication boleIntegralApplication)
    {

        //查询改用户可用积分
       Integer abScore = iSysUserService.selectUserById(SecurityUtils.getUserId()).getScore();
        startPage();
        List<BoleIntegralApplication> list = boleIntegralApplicationService.selectBoleIntegralApplicationList(boleIntegralApplication);
        return getDataTableAdditionInt(list,abScore);
    }

    /**
     * 导出积分申请列表
     */
    @PreAuthorize("@ss.hasPermi('bole:application:export')")
    @Log(title = "积分申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoleIntegralApplication boleIntegralApplication)
    {
        List<BoleIntegralApplication> list = boleIntegralApplicationService.selectBoleIntegralApplicationList(boleIntegralApplication);
        ExcelUtil<BoleIntegralApplication> util = new ExcelUtil<BoleIntegralApplication>(BoleIntegralApplication.class);
        util.exportExcel(response, list, "积分申请数据");
    }

    /**
     * 获取积分申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bole:application:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(boleIntegralApplicationService.selectBoleIntegralApplicationById(id));
    }

    /**
     * 新增积分申请
     */
    @Autowired
    private IBoleIntegralSeasonService boleIntegralSeasonService;
//    @PreAuthorize("@ss.hasPermi('bole:application:add')")
    @Log(title = "积分申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoleIntegralApplication boleIntegralApplication)
    {
        //如果applyDate为空就赋值现在
        if (boleIntegralApplication.getApplyDate() == null) {
            boleIntegralApplication.setApplyDate(new Date());
        }
        //赋予赛季
        boleIntegralApplication.setSeasonId(boleIntegralSeasonService.selectMaxSeason());
        // 获取当前登录用户的 userid
        Long userId = SecurityUtils.getUserId();
        boleIntegralApplication.setUserId(userId);
        return toAjax(boleIntegralApplicationService.insertBoleIntegralApplication(boleIntegralApplication));
    }

    /**
     * 修改积分申请
     */
    @PreAuthorize("@ss.hasPermi('bole:application:edit')")
    @Log(title = "积分申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoleIntegralApplication boleIntegralApplication)
    {
        return toAjax(boleIntegralApplicationService.updateBoleIntegralApplication(boleIntegralApplication));
    }

    /**
     * 删除积分申请
     */
    @PreAuthorize("@ss.hasPermi('bole:application:remove')")
    @Log(title = "积分申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(boleIntegralApplicationService.deleteBoleIntegralApplicationByIds(ids));
    }

@Log(title = "积分申请", businessType = BusinessType.DELETE)
@DeleteMapping("/user/{ids}")
@UserCheck
public AjaxResult userRemove(@PathVariable Long[] ids,Long userId)
{
//    System.out.println(userId);
//    System.out.println(ids.length);
//    System.out.println(ids[ids.length-1]);
//    return toAjax(1);
    return toAjax(boleIntegralApplicationService.deleteBoleIntegralApplicationByIds(ids));
}
}
